package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.polo.PoolPackage;
import com.xthink.common.vo.pool.LikeListPoolPackageVo;
import com.xthink.web.mapper.PoolPackageMapper;
import com.xthink.web.service.PoolPackageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PoolPackageServiceImpl extends ServiceImpl<PoolPackageMapper, PoolPackage> implements PoolPackageService {

    @Autowired
    private PoolPackageMapper packageMapper;

    //新建池套餐
    @Override
    public AjaxResult insertPoolPackage(PoolPackage poolPackage) {
        int insert = packageMapper.insert(poolPackage);
        if (insert == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    //套餐搜索及分页查询
    @Override
    public AjaxResult likeListPoolPackage(LikeListPoolPackageVo likeListPoolPackageVo) {
        Page<PoolPackage> page = new Page<>(likeListPoolPackageVo.getPage(),likeListPoolPackageVo.getSize());
        LambdaQueryWrapper<PoolPackage> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(likeListPoolPackageVo.getPackageName())
                , PoolPackage::getPackageName
                , likeListPoolPackageVo.getPackageName());
        wrapper.eq(likeListPoolPackageVo.getPackageType() != null
                , PoolPackage::getPackageType
                , likeListPoolPackageVo.getPackageType());
        wrapper.eq(likeListPoolPackageVo.getStatus() != null
                , PoolPackage::getStatus
                , likeListPoolPackageVo.getStatus());
        //被删除的套餐不显示
        wrapper.eq(PoolPackage::getDelFlag,0);
        Page<PoolPackage> poolPackagePage = packageMapper.selectPage(page, wrapper);
        List<PoolPackage> records = poolPackagePage.getRecords();
        return AjaxResult.success(records);
    }

    //上架池套餐
    @Override
    public AjaxResult poolPackagePutOn(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PoolPackage> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PoolPackage::getId,id).set(PoolPackage::getStatus,1);
            int update = packageMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("池套餐ID:"+id+"上架失败");
            }
        }
        return AjaxResult.success();
    }

    //下架池套餐
    @Override
    public AjaxResult poolPackageOff(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PoolPackage> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PoolPackage::getId,id).set(PoolPackage::getStatus,0);
            int update = packageMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("池套餐ID:"+id+"下架失败");
            }
        }
        return AjaxResult.success();
    }

    //删除池套餐
    @Override
    public AjaxResult poolPackageDelete(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PoolPackage> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PoolPackage::getId,id).set(PoolPackage::getDelFlag,1);
            int update = packageMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("池套餐ID:"+id+"删除失败");
            }
        }
        return AjaxResult.success();
    }

    //编辑池套餐
    @Override
    public AjaxResult poolPackageUpdate(PoolPackage poolPackage) {
        LambdaUpdateWrapper<PoolPackage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PoolPackage::getId,poolPackage.getId())
                .set(PoolPackage::getPackageType,poolPackage.getPackageType())
                .set(PoolPackage::getPackageName,poolPackage.getPackageName())
                .set(PoolPackage::getSaleName,poolPackage.getSaleName())
                .set(PoolPackage::getPackageSize,poolPackage.getPackageSize())
                .set(PoolPackage::getPackagePrice,poolPackage.getPackagePrice())
                .set(PoolPackage::getStatus,poolPackage.getStatus());
        int update = packageMapper.update(poolPackage, wrapper);
        if (update==0){
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
